package chapter14;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/22 21:29
 */

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Square.class, name = "square"), @JsonSubTypes.Type(value = Circle.class, name = "circle")
})
class Shape {
}

class Square extends Shape {
    private int l;

    public int getL() {
        return l;
    }

    public void setL(int l) {
        this.l = l;
    }
}

class Circle extends Shape {
    private int r;

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }
}

class ShapeManager {
    public List<Shape> shapes;

    public ShapeManager() {
        shapes = new ArrayList<>();
    }
}

public class JacksonTest3 {
    public static void main(String[] args) {
        ShapeManager manager = new ShapeManager();
        Circle circle = new Circle();
        Square square = new Square();

        manager.shapes.add(circle);
        manager.shapes.add(square);

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        try {
            String json = mapper.writeValueAsString(manager);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
